package com.theaf.basemodule.service.impl;

import com.theaf.basemodule.common.api.CarTypeService;
import com.theaf.basemodule.common.api.DataExService;
import com.theaf.basemodule.common.dto.car.BrandDTO;
import com.theaf.basemodule.common.dto.car.CarSysDTO;
import com.theaf.basemodule.common.dto.car.CarTypeDTO;
import com.theaf.basemodule.common.model.car.CarType;
import com.theaf.basemodule.common.vo.car.BrandVO;
import com.theaf.basemodule.common.vo.car.CarSysVO;
import com.theaf.basemodule.dao.car.CarTypeDAO;
import com.theaf.core.repo.specification.SqlBuilder;
import com.theaf.core.repo.specification.SqlValue;
import com.theaf.core.utils.BeanMapper;
import com.theaf.core.utils.BeanUtils;
import com.theaf.core.utils.SmExceptionHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:songwenke
 * @Description:车型服务实现
 * @Date:create in 9:40 2018/3/19
 * @Modified By:
 **/
@Service
public class CarTypeServiceImpl implements CarTypeService{
    @Resource
    private CarTypeDAO carTypeDAO;

    @Resource
    private DataExService dataExService;

    @Override
    public List<BrandDTO> findAllBrand(String useType) {
        return (List<BrandDTO>)carTypeDAO.findAll(SqlBuilder.start("type",CarType.TYPE_BRAND).and("useType", SqlValue.parse(useType)).gen())
                .parallelStream().map(n-> BeanMapper.map(n,BrandDTO.class)).collect(Collectors.toList());
    }

    @Override
    public List<CarSysDTO> findCarSysByBrand(long id,String useType) {
        return (List<CarSysDTO>)carTypeDAO.findAll(SqlBuilder.start("type",CarType.TYPE_SYS)
                .and("pid",id).and("useType",SqlValue.parse(useType)).gen())
                .parallelStream().map(n-> {
                    CarSysDTO carSysDTO = BeanMapper.map(n,CarSysDTO.class);
                    carSysDTO.setBrandName(carTypeDAO.findOne(
                            SqlBuilder.start("id",
                                    ((CarType)n).getPid()).gen()).getName());
                    return carSysDTO;
                }).collect(Collectors.toList());
    }

    @Override
    public List<CarTypeDTO> findCarTypeBySys(long id,String useType) {
        return (List<CarTypeDTO>)carTypeDAO.findAll(SqlBuilder.start("type",CarType.TYPE_MODEL)
                .and("pid",id).and("useType",SqlValue.parse(useType)).gen())
                .parallelStream().map(n-> {
                    CarTypeDTO carTypeDTO = BeanMapper.map(n, CarTypeDTO.class);
                    carTypeDTO.setCarTypeName(carTypeDAO.findOne(
                            SqlBuilder.start("id",
                                    ((CarType)n).getPid()).gen()).getName());
                    return carTypeDTO;
                }).collect(Collectors.toList());
    }

    @Override
    public CarTypeDTO findCarTypeById(Long id){
        CarType carType = carTypeDAO.findOne(id);
        CarTypeDTO carTypeDTO = BeanMapper.map(carType,CarTypeDTO.class);
        //dataExService.setDTODataFromPo("car_type",null,carTypeDTO,carType.getOthers());
        if(carType.getPid()!=null) {
            carTypeDTO.setCarTypeName(carTypeDAO.findOne(
                    SqlBuilder.start("id",
                            carType.getPid()).gen()).getName());
        }
        return carTypeDTO;
    }

    @Override
    public CarSysDTO findCarSysById(Long id){
        CarType carType = carTypeDAO.findOne(id);
        CarSysDTO carTypeDTO = BeanMapper.map(carType,CarSysDTO.class);
        carTypeDTO.setBrandName(carTypeDAO.findOne(
                SqlBuilder.start("id",
                        carType.getPid()).gen()).getName());
        return carTypeDTO;
    }

    @Override
    public long save(Serializable vo) {
        CarType carType = null;
        try {
            carType = carTypeDAO.findByIdNewIfisNull((Long)BeanUtils.getProperty(vo,"id"));
            BeanMapper.copy(vo,carType);
            if(carType.getPid()!=null&&!carTypeDAO.exists(carType.getPid())){
                throw SmExceptionHelper.sysCheckException(carType.getPid()+"不存在");
            }
            if(vo instanceof BrandVO){
                carType.setType(CarType.TYPE_BRAND);
            }else if(vo instanceof CarSysVO){
                carType.setType(CarType.TYPE_SYS);
            }else{
                carType.setType(CarType.TYPE_MODEL);
            }
            carTypeDAO.save(carType);
            /*if(vo instanceof CarTypeVO) {
                dataExService.savePoOther(carType.getId(), vo, "car_type",null);
            }*/
            return carType.getId();
        } catch (Exception e) {
            throw SmExceptionHelper.sysCheckException(e.getMessage());
        }
    }

    @Override
    public void delete(Long id) {
        carTypeDAO.delete(id);
    }
}
